package ebedynky.services;

import ebedynky.beans.HibernateUtil;
import ebedynky.beans.Grange;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 * @author davidon1
 */
public class GrangeService {

    /**
     * Seesion - current connection to the db.
     */
    private Session session;

    /**
     * Constructor, retrieves the session (connection).
     */
    public GrangeService() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * Closes the session (connection).
     * @return success/failure.
     */
    public boolean close() {
        this.session.close();
        return !(this.session.isOpen());
    }

    /**
     * Restarts the session (connection).
     * @return success/failure.
     */
    public boolean restart() {
        this.session.close();
        if (this.session.isOpen()) {
            throw new IllegalStateException("Session could not be closed.");
        }
        this.session = HibernateUtil.getSessionFactory().openSession();
        return this.session.isOpen();
    }

    /**
     * Adds Grange to the db.
     * @param grange Grange to be inserted
     * @return ID of the added Grange
     */
    public int addGrange(Grange grange) {
        session.beginTransaction();
        int id = getMaxGrangeID() + 1;
        grange.setGrangeID(id);
        id = (Integer) session.save(grange);
        session.getTransaction().commit();
        return (id);
    }

    /**
     * Retrieves the maximum id currently in the database of Granges.
     * @return Max ID
     */
    public int getMaxGrangeID() {
        String hql = "select max(g.grangeID) from Grange g";
        List list = session.createQuery(hql).list();
        if (list.get(0) != null) {
            Integer maxgrangeid = (Integer) list.get(0);
            return maxgrangeid;
        } else {
            return 0;
        }
    }

    /**
     * Retrieves a Grange identified by the ID.
     * @param id of the Grange
     * @return Grange with the ID given (null if not found)
     */
    public Grange getGrangeByID(int id) {
        session.beginTransaction();
        Grange grange = (Grange) session.get(Grange.class, new Integer(id));
        session.getTransaction().commit();
        return (grange);
    }

    /**
     * Retrieves a list of all Granges stored in the db.
     * @return List of all Granges (null if no Granges)
     */
    public List<Grange> getAllGranges() {
        session.beginTransaction();
        List<Grange> grangeList = session.createQuery("from Grange").list();
        session.getTransaction().commit();
        return (grangeList);
    }

    /**
     * Updates a specified Grange identified by ID.
     * @param newGrange an updated Grange
     */
    public void updateGrange(Grange newGrange) {
        session.beginTransaction();
        Grange grange = (Grange) session.get(Grange.class, new Integer(newGrange.getGrangeID()));
        grange.setAgreements(newGrange.getAgreements());
        grange.setCustomers(newGrange.getCustomers());
        grange.setLeader(newGrange.getLeader());
        grange.setMaxCustomers(newGrange.getMaxCustomers());
        grange.setMaxSuppliers(newGrange.getMaxSuppliers());
        grange.setSuppliers(newGrange.getSuppliers());
        session.flush();
        session.getTransaction().commit();
        session.clear();
    }

    /**
     * Removes the Grange from db.
     * @param id Grange ID for removal
     * @return true if successful, false otherwise
     */
    public boolean deleteGrange(int id) {
        try {
            session.beginTransaction();
            Grange grange = (Grange) session.get(Grange.class, new Integer(id));
            session.delete(grange);
            session.getTransaction().commit();
            return (true);
        } catch (HibernateException ex) {
            return (false);
        }
    }
}